<?php
defined('BASEPATH') or exit('No direct script access allowed');

use QCloud_WeApp_SDK\Constants as Constants;
use \QCloud_WeApp_SDK\Auth\LoginService as LoginService;

class User extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('form_validation'); //加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('common');
        $this->load->model('Customer_model');
    }

    /**
     * @SWG\Get(
     *     summary="用户授权后再次登录",
     *     path="/user",
     *     tags={"微信授权登录"},
     *     operationId="7",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="用户类型",
     *         in="query",
     *         name="type",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    public function index()
    {
        $result = LoginService::check();
        if ($result['loginState'] === Constants::S_AUTH) {
            $result['userinfo']['uid']        = $result['uid'];
            $result['userinfo']['tel']        = $result['tel'];
            $result['userinfo']['phone']      = $result['phone'];
            $result['userinfo']['status']     = $result['status'];
            $result['userinfo']['sessionKey'] = $result['sessionKey'];
            //根据sessionKey,type判断客户是否绑定商户
            if ($result['type'] == 2) {
                $row = $this->Customer_model->isBinding($result['sessionKey']);
                if ($row) {
                    $result['userinfo']['isbinding'] = 1;
                } else {
                    $result['userinfo']['isbinding'] = 0;
                }
            }
            $this->json([
                'code' => 2000,
                'data' => $result['userinfo'],
            ]);
        } else {
            $this->json([
                'code' => 4004,
                'data' => [],
            ]);
        }
    }

    /**
     * @SWG\Get(
     *     summary="小程序授权手机号码解密",
     *     path="/decryption",
     *     tags={"微信授权登录"},
     *     operationId="29",
     *     produces={"application/xml", "application/json"},
     *     @SWG\Parameter(
     *         description="sessionKey",
     *         in="query",
     *         name="sessionKey",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="encryptedData",
     *         in="query",
     *         name="encryptedData",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Parameter(
     *         description="iv",
     *         in="query",
     *         name="iv",
     *         required=true,
     *         type="string",
     *     ),
     *     @SWG\Response(
     *         response=200,
     *         description="成功",
     *     ),
     *     @SWG\Response(
     *         response="401",
     *         description="参数不全",
     *     ),
     * )
     */
    public function decryption()
    {
        require APPPATH . '/libraries/decrypt/wxBizDataCrypt.php';
        require APPPATH . '/libraries/pay/WxPay.Config.php';
        $post          = $this->input->post();
        $appid         = WxPayConfig::APPID;
        $sessionKey    = $post['sessionKey'];
        $encryptedData = $post['encryptedData'];
        $iv            = $post['iv'];
        $pc            = new WXBizDataCrypt($appid, $sessionKey);
        $errCode       = $pc->decryptData($encryptedData, $iv, $data);
        if ($errCode == 0) {
            $phoneNumber = json_decode($data,true)['phoneNumber'];
            $where = array('session_key' => $post['sessionKey']);
            $this->db->where($where)->update('app_user', array('phone' => $phoneNumber));
            send_json(array('code' => '2000', 'data' => '', 'msg' => '解密成功'));
        } else {
            send_json(array('code' => '4002', 'data' => $errCode, 'msg' => '解密失败'));
        }
    }
}
